<?php
class filtros{

	public $base_url 		= "";
	public $description_url = "";
	public $page 			= 1;
	public $url				= "";
	public $mandatory_filters_string	= '';
	public $aditional_filters_string	= '';
	
	
	function get_where_clause($selected_filter=''){
		switch( $this->filters_type ){
			case 'links' : return $this->links_get_where_clause($selected_filter);
			case 'check' : return $this->check_get_where_clause($selected_filter);
		}
	}
	
	function complete_url_on_remove($parametros,$url_den,$additional_filters_string,$filtros_adicionales,$url_completa=''){
		switch( $this->filters_type ){
			case 'links' : return $this->links_complete_url_on_remove($parametros,$url_den,$additional_filters_string,$filtros_adicionales,$url_completa);
			case 'check' : return $this->check_complete_url_on_remove($parametros,$url_den,$additional_filters_string,$filtros_adicionales);
		}
	}
	
	function create_items_lists($url_den,$additional_filters_string){
		switch( $this->filters_type ){
			case 'links' : return $this->links_create_items_lists($url_den,$additional_filters_string);
			case 'check' : return $this->check_create_items_lists($url_den,$additional_filters_string);
		}
	}

	
	
	function __construct($currency){
	
		$this->currency 		= $currency;
		
		
		$parametros = $this->get_parameters($this->base_url);
		
		foreach ($this->filters_list as $current_key => $current_filter){
			$field_denomination = $current_filter['field_denomination'];
			if (isset($parametros[$field_denomination]) && $parametros[$field_denomination] != ""){
			
				$this->filters_list[$current_key]['selected'] 					= true;
				$this->filters_list[$current_key]['selected_value'] 			= $parametros[$field_denomination];
				$this->filters_list[$current_key]['display_text_on_selected'] 	= $this->get_display_text($field_denomination,$parametros[$field_denomination]);
				
			}
			elseif (isset($filtros_adicionales[$field_denomination]) && $filtros_adicionales[$field_denomination] != ""){
				$this->filters_list[$current_key]['selected'] 		= true;
				if ($current_filter['filter_type'] == 'range'){
					if ($filtros_adicionales[$field_denomination] != ""){
						$valor 			= $this->parse_range_value($filtros_adicionales[$field_denomination]);
					}
				}
				else{
					$valor = $filtros_adicionales[$field_denomination];
				}
				$this->filters_list[$current_key]['selected_value']				= $valor;
				$this->filters_list[$current_key]['display_text_on_selected'] 	= $this->get_display_text($field_denomination,$valor);
			}
		}
		
		
		$url_completa 				= $this->get_filter_url($this->url_den,null,null,$this->aditional_filters_string);		
		
		$this->complete_url_on_remove($parametros,$this->url_den,$this->additional_filters_string,$this->filtros_adicionales,$url_completa);
		
		$this->create_items_lists($this->url_den,$this->additional_filters_string);
		
	}
	
	

	function remove_additional_param($string,$param){
		$paramPos 			= strpos($string,'/'.$param.'/');
		// echo $string . '<br>' . $paramPos;
		//die($paramPos);
		$end_string 		= substr($string,$paramPos,strlen($string)-1);
		$barras_contadas 	= 0;
		for ($i=0;$i<strlen($end_string)-1;$i++){
			if ($end_string[$i] == '/'){
				if ($barras_contadas == 2){
					$lastBarPos = $i;
					break;
				}
				$barras_contadas++;
			}

		}
		$definitive_string = substr($end_string,0,$lastBarPos);
		return str_replace($definitive_string,'',$string);
	}
	
	function parse_range_value($valuesString){
		$array = explode('-',$valuesString);
		if( count($array) == 2 && $array[1] != ''){
			$return['min'] = $array[0];
			$return['max'] = $array[1];
		}
		else{
			if (substr($array[0],strlen($array[0])-1,1) == '+'){
				$return['min'] = "";
				$return['max'] = substr($array[0],0,strlen($array[0])-1);
			}else{
				$return['min'] = substr($array[0],0,strlen($array[0]));
				$return['max'] = "";
			}
		}
		return $return;
	}
	
	function links_get_where_clause($selected_filter=''){
		$return_string = " ";
		foreach ($this->filters_list as $filter){
			if ($filter['field_denomination'] != $selected_filter && $filter['selected'] == true){
				switch($filter['filter_type']){
					case 'text' 		: $return_string .= 'AND ' . $filter['id_field_on_table'] . ' like "%' . $filter['selected_value'] . '%" '; break;
					case 'items_list' 	: $return_string .= 'AND ' . $filter['id_field_on_table'] . ' = "' . $filter['selected_value'] . '" '; break;
					case 'range' 		: 
						if ($filter['selected_value']['min'] != ""){
							$return_string .= 'AND ' . $filter['id_field_on_table'] . ' >= ' . $filter['selected_value']['min'] . ' ';
						};
						if ($filter['selected_value']['max'] != ""){
							$return_string .= 'AND ' . $filter['id_field_on_table'] . ' <= ' . $filter['selected_value']['max'] . ' ';
						};
						break;
				}
			}
		}
		return $return_string . ' AND ' . $this->campo_precio . ' != "" ';
	}
	
	function links_complete_url_on_remove($parametros,$url_den,$additional_filters_string,$filtros_adicionales,$url_completa){
		foreach ($this->filters_list as $current_key => $current_filter){
			$field_denomination = $current_filter['field_denomination'];
			if (isset($parametros[$field_denomination]) && $parametros[$field_denomination] != ""){
				$this->filters_list[$current_key]['url_on_remove'] 	= $this->get_filter_url($url_den,$current_key,null,$additional_filters_string);
			}
			elseif (isset($filtros_adicionales[$field_denomination]) && $filtros_adicionales[$field_denomination] != ""){
				$this->filters_list[$current_key]['url_on_remove']  = $this->remove_additional_param($url_completa,$this->filters_list[$current_key]['field_denomination']);				
			}
		}
	}
	
	function links_create_items_lists($url_den,$additional_filters_string){
		foreach ($this->filters_list as $current_key => $current_filter){
			if ($current_filter['filter_type'] == 'items_list' && !$current_filter['selected']){
				$list = $this->get_items_list($current_filter);
				
				foreach($list as $item){
					$precio_minimo = '';
					if (isset($item['precio_minimo'])){
						$precio_minimo = $item['precio_minimo'];
					}
					$this->filters_list[$current_key]['items_list'][] = array(
						'url' => $this->get_filter_url($url_den,$current_key,$item['value'],$additional_filters_string),
						'value' => $item['value'],
						'display_value' => $item['display_value'],
						'quantity' => $item['cantidad'],
						'min_price' => $precio_minimo,
						'link_display_text' 	=> $this->get_display_value($current_key,$item['value'],$item['display_value'],$current_filter['link_display_text'])
					);
				}
			}
		}
	}
	function check_get_where_clause($selected_filter=''){
		$return_string = " ";
		foreach ($this->filters_list as $filter){
			if ($filter['field_denomination'] != $selected_filter && $filter['selected'] == true){
				switch($filter['filter_type']){
					case 'text' 		: $return_string .= 'AND ' . $filter['id_field_on_table'] . ' like "%' . $filter['selected_value'] . '%" '; break;
					case 'items_list' 	: 
						$selected_values_string = '"' . implode('","',$filter['selected_value']) . '"';
						$return_string .= 'AND ' . $filter['id_field_on_table'] . ' IN (' . $selected_values_string . ') '; 
						break;
					case 'range' 		: 
						if ($filter['selected_value']['min'] != ""){
							$return_string .= 'AND ' . $filter['id_field_on_table'] . ' >= ' . $filter['selected_value']['min'] . ' ';
						};
						if ($filter['selected_value']['max'] != ""){
							$return_string .= 'AND ' . $filter['id_field_on_table'] . ' <= ' . $filter['selected_value']['max'] . ' ';
						};
						break;
				}
			}
		}
		return $return_string . ' AND ' . $this->campo_precio . ' != "" ';
	}
	
	function check_complete_url_on_remove($parametros,$url_den,$additional_filters_string,$filtros_adicionales){
		foreach ($this->filters_list as $current_key => $current_filter){
			$field_denomination = $current_filter['field_denomination'];
			if ($current_filter['selected'] ){
				$this->filters_list[$current_key]['url_on_remove'] 	= $this->get_filter_url($url_den,$current_filter['field_denomination'],$additional_filters_string);
			}
		}
	}

	function check_create_items_lists($url_den,$additional_filters_string){
		foreach ($this->filters_list as $current_key => $current_filter){
			if ($current_filter['filter_type'] == 'items_list'){
				$list = $this->get_items_list($current_filter);
				foreach($list as $item){
					$checked = false;
					if (in_array($item['value'],$current_filter['selected_value'])){
						$checked = true;
					}
					$precio_minimo = '';
					if (isset($item['precio_minimo'])){
						$precio_minimo = $item['precio_minimo'];
					}
					$this->filters_list[$current_key]['items_list'][] = array(
						'check_display_text' 	=> $this->get_display_value($current_key,$item['value'],$item['display_value'],$current_filter['check_display_text']),
						//'check_display_text' 	=> str_replace('<value>',$item['value'],$current_filter['check_display_text']),
						'url' 		=> $this->get_filter_url($url_den,$current_key,$item['value'],$additional_filters_string),
						'value' 	=> $item['value'],
						'quantity' 	=> $item['cantidad'],
						'min_price'	=> $precio_minimo,
						'checked' 	=> $checked
					);
				}
			}
		}
	}
	
	function init_aditional_filters($url){
		
		$filtros 				= $this->filtros_adicionales;
		
		$filtros_adicionales	= array();
		
		$url_parts 				= explode('/',$url);
		
		if ($url_parts[count($url_parts)-2] == 'pag'){
			$this->page = $url_parts[count($url_parts)-1];
			unset($url_parts[count($url_parts)-1]);
			unset($url_parts[count($url_parts)-1]);
		}
		
		$this->url = implode('/',$url_parts);
		
		$this->description_url = '/' . $url_parts[count($url_parts)-1];
		
		unset($url_parts[count($url_parts)-1]);
		
		foreach($this->filtros_obligatorios as $key => $value){
			$filtros_obligatorios_keys[] = $key;
		}
		
		foreach ($filtros_obligatorios_keys as $filtro_ob){
			$filtros_obligatorios[$filtro_ob] = ''; 
		}
		foreach ($filtros as $filtro){
			$filtros_adicionales[$filtro] = ''; 
		}
		
		$i=count($url_parts)-2;
		$lastBasicIndex = count($url_parts)-1;
		$mandatory_filters_string = '';
					
		while ($i>=0) {
			if (in_array($url_parts[$i],$filtros_obligatorios_keys)){
				$filtros_obligatorios[$url_parts[$i]] = $url_parts[$i+1];
			}else{
				$lastBasicIndex = $i+1;
				break;
			}
			$i=$i-2;
		}
		
		foreach ($filtros_obligatorios as $current_key => $current_value){
			$mandatory_filters_string .= '/' . $current_key . '/' . $current_value;
		}
		$this->filtros_obligatorios 	= $filtros_obligatorios;
		$this->mandatory_filters_string = $mandatory_filters_string;
		
		// die_dump($url_parts,'main_51',false);
		// die_dump($filtros,'main_52',false);
		// die_dump($i,'main_53');
						
		while ($i>=0) {
			if (in_array($url_parts[$i],$filtros)){
				$filtros_adicionales[$url_parts[$i]] = $url_parts[$i+1];
			}else{
				$lastBasicIndex = $i+1;
				break;
			}
			$i=$i-2;
		}
		
		
		$this->base_url = "";
		$additional_filters_string 	= "";
		for ($i=0;$i<count($url_parts);$i++){
			if ($i <= $lastBasicIndex ){
				if (!empty($this->base_url)){ $this->base_url .= '/'; }
				$this->base_url .= $url_parts[$i];   
			}else{
				if (!empty($additional_filters_string)){ $additional_filters_string .= '/'; }
				$additional_filters_string 	.= $url_parts[$i];		
			}
		}
		
		$this->additional_filters_string = $additional_filters_string;
	}
}
?>